package com.drinks.yiniang.ui.activity.user.wine;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.io.api.WineListApiIO;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

/**
 * 新建酒单页面
 *
 * @author han
 * @date 2019/09/03
 */
public class CreateWineListActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    //nav
    private NavigationBarLayout nav_bar_create_wine_list;
    //标题
    private EditText et_create_wine_list_title;
    //描述
    private EditText et_create_wine_list_describe;
    //设为私密
    private TextView tv_create_wine_list_private;
    //完成
    private Button btn_create_wine_list_done;

    //当前私密状态, true->设为私密
    private boolean isPrivate = false;

    @Override
    protected void initView() {
        nav_bar_create_wine_list = findViewById(R.id.nav_bar_create_wine_list);

        et_create_wine_list_title = findViewById(R.id.et_create_wine_list_title);
        et_create_wine_list_describe = findViewById(R.id.et_create_wine_list_describe);

        tv_create_wine_list_private = findViewById(R.id.tv_create_wine_list_private);

        btn_create_wine_list_done = findViewById(R.id.btn_create_wine_list_done);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;
    }

    @Override
    protected void initListener() {
        nav_bar_create_wine_list.setOnNavgationBarClickListener(this);

        tv_create_wine_list_private.setOnClickListener(this);
        btn_create_wine_list_done.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.tv_create_wine_list_private://设为私密
                switchPrivateStatus();
                break;
            case R.id.btn_create_wine_list_done://完成
                doDone();
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_create_wine_list;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    //切换私密状态
    private void switchPrivateStatus() {
        if (isPrivate) {//当前为私密状态, 点击切换为非私密
            isPrivate = false;
            tv_create_wine_list_private.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_choose_n, 0, 0, 0);
        } else {
            isPrivate = true;
            tv_create_wine_list_private.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_choose_p, 0, 0, 0);
        }
    }

    //完成
    private void doDone() {
        String title = StringUtils.getEditTextString(et_create_wine_list_title.getText());

        if (StringUtils.isEmpty(title)) {
            ToastUtils.showShort(getString(R.string.text_hint_input_title));
            return;
        }

        showLoadingDialog();

        String desc = StringUtils.getEditTextString(et_create_wine_list_describe.getText());
        String isPublic = isPrivate ? "0" : "1";

        WineListApiIO.getInstance().createWineList(title, desc, isPublic, "0", new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                setResult(Constants.RESULT_CODE_NORMAL);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
